<template>
  <div class="PageModel">
    <el-dialog
      :title="creatOrEdit + title"
      v-model="dialogVisible"
      width="30%"
      center
      destroy-on-close
    >
      <MyForm
        v-bind="modelConfig"
        v-model="formData"
        style="padding: 0"
        @changeIsHidden="changeIsHidden"
        :pageName="pageName"
      ></MyForm>
      <slot></slot>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleConfirmClick">
            确 定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from 'vue'
import { useStore } from 'vuex'
import MyForm from '@/base-ui/Form/Form.vue'
import { pageModelConfig } from '@/views/main/system/menu/pageModelConfig'
import { menuTypeChange } from '@/utils/menuTypeChange'
export default defineComponent({
  components: {
    MyForm
  },
  props: {
    modelConfig: {
      type: Object,
      required: true
    },
    defaultInfo: {
      type: Object,
      default: () => ({})
    },
    otherInfo: {
      type: Object,
      default: () => ({})
    },
    pageName: {
      type: String,
      require: true
    },
    title: {
      type: String,
      require: true
    },
    creatOrEdit: {
      type: String,
      require: true
    }
  },
  setup(props) {
    let dialogVisible = ref(false)
    const formData = ref<any>({})

    const changeIsHidden = async (val: any) => {
      dialogVisible.value = false
      for (const item of pageModelConfig.formItems) {
        if (item.field === 'parentId') {
          item.options = await menuTypeChange(val, pageModelConfig)
          console.log(item.options)
        }
      }
      dialogVisible.value = true
    }
    watch(
      () => props.defaultInfo,
      (newValue) => {
        for (const item of props.modelConfig.formItems) {
          formData.value[`${item.field}`] = newValue[`${item.field}`]
        }
      }
    )
    // 点击确定按钮的逻辑
    const store = useStore()
    const handleConfirmClick = () => {
      console.log(1)

      dialogVisible.value = false
      for (const item of props.modelConfig.formItems) {
        if (item.options) {
          for (const i of item.options) {
            if (formData.value[item.field] === i.title) {
              formData.value[item.field] = i.value
            }
          }
        }
      }
      if (Object.keys(props.defaultInfo).length) {
        // 编辑
        store.dispatch('table/editPageDataAction', {
          pageName: props.pageName,
          editData: { ...formData.value, ...props.otherInfo },
          id: props.defaultInfo.id
        })
      } else {
        // 新建
        store.dispatch('table/createPageDataAction', {
          pageName: props.pageName,
          newData: { ...formData.value, ...props.otherInfo }
        })
      }
    }
    return {
      changeIsHidden,
      dialogVisible,
      formData,
      handleConfirmClick
    }
  }
})
</script>

<style scoped lang="less">
.PageModel {
}
</style>
